﻿using Framework.Common;
using Framework.Common.Exceptions;
using Framework.Common.Paged;
//*********************************************************************
//
//  文件名：SpecialController
//
//  作者： LW
//
//  作用：
//
//  日期：2015/9/22 10:50:46
//
//*********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using YouTravel.DomainService;
using YouTravel.Framework.Enums;
using YouTravel.Framework.ResourceMessage;
using YouTravel.QueryModels;
using YouTravel.ViewModels;
using System.Web.Script.Serialization;

namespace Application.WebAPI.Controllers
{
    /// <summary>
    /// SpecialController
    /// 专线管理
    /// </summary>
    [RoutePrefix("api/special")]
    public class SpecialController : BaseController
    {
        ISpecialService specialService;
        DataResult dataResult = new DataResult() { Code = ResponseStatusCode.Error, Msg = "" };

        public SpecialController(ISpecialService _specialService)
        {
            this.specialService = _specialService;
        }

        /// <summary>
        /// 专线列表
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("speciallist")]
        public DataResult GetList([FromBody]PagedParam<SpecialMainQuery> queryCond)
        {
            queryCond.QueryDto = queryCond.QueryDto ?? new SpecialMainQuery();
            var user = LoginUser;
            if (!this.IsSystemAdmin)
            {
                if (LoginUser.BranchId.HasValue)
                {
                    queryCond.QueryDto.BranchID = LoginUser.BranchId.Value;
                }
            }
            var list = specialService.GetList(queryCond);
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Data = list;
            return dataResult;
        }

        /// <summary>
        /// 专线创建
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost, Route("createspecial")]
        public DataResult Create([FromBody]SimpleModel m)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();

            DateTime dt = DateTime.Now;

            SpecialCreateViewModel model = js.Deserialize<SpecialCreateViewModel>(m.ID);
            var user = LoginUser;
            model.BranchId = user.BranchId.Value;
            model.Status = SpecialStatus.Normal;
            model.Creater = user.ID;
            model.CreaterName = user.Name;
            model.CreateTime = dt;
            model.ContractStartDate = dt;

            this.specialService.CreateSpecial(model);
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            return dataResult;
        }

        /// <summary>
        /// 专线编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("editspecial")]
        public DataResult Edit([FromBody]SpecialEditViewModel model)
        {
            var user = LoginUser;
            model.Updater = user.ID;
            model.UpdaterName = user.Name;
            model.UpdateTime = DateTime.Now;

            this.specialService.ModifySpecial(model);
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            return dataResult;
        }

        /// <summary>
        /// 专线销户
        /// </summary>
        /// <param name="ID">专线ID</param>
        /// <returns></returns>
        [HttpGet, Route("cancelspecial")]
        public DataResult Cancel([FromUri]string ID)
        {
            this.specialService.CancelSpecial(int.Parse(ID), LoginUser.ID);
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            return dataResult;
        }

        /// <summary>
        /// 专线详情
        /// </summary>
        /// <param name="ID">专线ID</param>
        /// <returns></returns>
        [HttpGet, Route("detailspecial")]
        public DataResult Detail([FromUri]string ID)
        {
            var tmp = this.specialService.GetDetailSpecial(int.Parse(ID));
            dataResult.Data = tmp;
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            return dataResult;
        }

        /// <summary>
        /// 专线基本信息
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("basicspecial")]
        public DataResult BasicDetail()
        {
            var user = LoginUser;
            if (user.Special == null || user.Type != UserType.SpecialLine)
                throw new DataOperationPermissions("非专线用户不能使用此功能");

            var tmp = this.specialService.GetDetailSpecial(user.Special.ID);
            dataResult.Data = tmp;
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            return dataResult;
        }
    }
}